const AgoraAPIQueue = require('../AgoraAPIQueue');
const AgoraAPIBase = require('../AgoraAPIBase');
const errors = require('../error');
const logger = require('../../utils').getLogger(__filename);

const queue = new AgoraAPIQueue({ logger });

module.exports = class APIStop extends AgoraAPIBase {
	constructor(data, modal) {
		super(data, modal);
		this.logger = logger
	}

	async run() {
		return queue.push(this.createArgs()).then(j => {
			if (j.resourceId && j.sid) {
				return j
			}
			return Promise.reject(new errors.ResultFormat({ data: this.data, response: j }));
		});
	}

	createArgs() {
		let { channelName, rid, sid, resourceId } = this.data;
		let path = `/v1/apps/${this.appId}/cloud_recording/resourceid/${resourceId}/sid/${sid}/mode/${this.modal}/stop`;
		let body = {
			cname: channelName,
			uid: rid.toString(),
			clientRequest: {}
		};

		let options = {
			body: JSON.stringify(body)
		};
		return { url: path, options, type: 'post' };
	}

};
